package com.xcf.controller.business.common;


import java.net.URLDecoder;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.commons.CommonsMultipartFile;
import org.springframework.web.servlet.ModelAndView;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.xcf.controller.base.BaseController;
import com.xcf.controller.base.ExportDataHandler;
import com.xcf.entity.Page;
import com.xcf.service.business.common.boatcompanyfixedsurcharge.BoatCompanyFixedSurchargeService;
import com.xcf.util.Const;
import com.xcf.util.DateUtil;
import com.xcf.util.FileDownload;
import com.xcf.util.FileUpload;
import com.xcf.util.ObjectExcelRead;
import com.xcf.util.PageData;
import com.xcf.util.PathUtil;
import com.xcf.util.ResponseResult;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
/** 
 * 类名称：BoatCompanyFixedSurchargeController
 * 创建人：tanwei
 * 更新时间：2017年12月09日
 * @version
 */
@Controller
@RequestMapping(value="/boatCompanyFixedSurcharge")
public class BoatCompanyFixedSurchargeController extends BaseController implements ExportDataHandler{
	@Resource
	private BoatCompanyFixedSurchargeService boatCompanyFixedSurchargeService;
	/**
	 * 列表页面
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/list")
	public ModelAndView findInfo()throws Exception{
		ModelAndView view = new ModelAndView();
		view.setViewName("/business/common/boatcompanyfixedsurcharge/boat_company_fixed_surcharge");
		return view;
		
	}
	/**
	 * 海船固定附加费用
	 * @throws Exception
	 */
	@RequestMapping(value="/BoatCompanyFixedSurchargeInfo")
	public void BoatCompanyFixedSurchargeInfo(HttpServletResponse response)throws Exception{
		PageData pd = new PageData();
		pd = this.getPageData();
		String BOAT_COMPANY=null;
		if(pd.getString("BOAT_COMPANY")!=null && pd.getString("BOAT_COMPANY")!=""){
			BOAT_COMPANY=URLDecoder.decode(pd.getString("BOAT_COMPANY"), "utf-8");
			pd.put("BOAT_COMPANY", BOAT_COMPANY);
		}
		String ROUTE_NAME=null;
		if(pd.getString("ROUTE_NAME")!=null && pd.getString("ROUTE_NAME")!=""){
			ROUTE_NAME=URLDecoder.decode(pd.getString("ROUTE_NAME"), "utf-8");
			pd.put("ROUTE_NAME", ROUTE_NAME);
		}
		String FEE_NAME=null;
		if(pd.getString("FEE_NAME")!=null && pd.getString("FEE_NAME")!=""){
			FEE_NAME=URLDecoder.decode(pd.getString("FEE_NAME"), "utf-8");
			pd.put("FEE_NAME", FEE_NAME);
		}
		List<PageData> entityMainTain = boatCompanyFixedSurchargeService.findInfo(pd);
		JSONArray json = new JSONArray();
		for(PageData p : entityMainTain){
			 JSONObject jo = new JSONObject();

			 jo.put("ID", p.get("ID"));
			 jo.put("BOAT_COMPANY", p.getString("BOAT_COMPANY"));
			 jo.put("SETTLE_UNIT", p.getString("SETTLE_UNIT"));
			 jo.put("FEE_NAME", p.getString("FEE_NAME"));
			 jo.put("FEE_PRICE", p.get("FEE_PRICE"));
			 jo.put("CURRENCY_TYPE", p.getString("CURRENCY_TYPE"));
			 jo.put("CHARG_STANDARD", p.getString("CHARG_STANDARD"));
			 jo.put("BOX_TYPE", p.getString("BOX_TYPE"));
			 jo.put("REMARK", p.getString("REMARK"));
			 jo.put("ENABLE", p.get("ENABLE"));
		
			 jo.put("SEARCH_CODE", p.getString("SEARCH_CODE"));
			 jo.put("COMPANY_CODE", p.getString("COMPANY_CODE"));
			 jo.put("ROUTE_CODE", p.getString("ROUTE_CODE"));
			 jo.put("ROUTE_NAME", p.getString("ROUTE_NAME"));
			/**
			 * @author zhouheyou
			 * 更新时间：2018年1月16日
			 */
			 if(p.get("START_PRICE_TIME")!=null && p.get("START_PRICE_TIME")!=""){
				 SimpleDateFormat formate= new SimpleDateFormat("yyyy-MM-dd");
				 String str =formate.format(p.get("START_PRICE_TIME"));
				 jo.put("START_PRICE_TIME", str);//开始时间
			 }else{
				 jo.put("START_PRICE_TIME", p.get("START_PRICE_TIME"));//开始时间
			 }
			 if(p.get("END_PRICE_TIME") !=null && p.get("END_PRICE_TIME")!=""){
				 SimpleDateFormat formate= new SimpleDateFormat("yyyy-MM-dd");
				 String str =formate.format(p.get("END_PRICE_TIME"));
				 jo.put("END_PRICE_TIME", str);//结束时间
			 }else{
				 jo.put("END_PRICE_TIME", p.get("END_PRICE_TIME"));//结束时间 
			 }
			
	            json.add(jo);
		}
		response.setContentType("text/html; charset=utf-8");
		response.getWriter().write("{"+"\""+"total"+"\""+":"+"\""+"5"+"\""+","+"\""+"root"+"\""+":"+json.toString()+"}");				
		response.getWriter().flush();
	}
	/**
	 * 查找箱型
	 * @throws Exception
	 */
	@RequestMapping(value="/findBoxType")
	public void findBoxType(HttpServletResponse response)throws Exception{
		List<PageData> entityMainTain = boatCompanyFixedSurchargeService.findBoxType();
		JSONArray json = new JSONArray();
		for(PageData p : entityMainTain){
			 JSONObject jo = new JSONObject();
			 jo.put("BOX_TYPE_NAME", p.get("BOX_TYPE_NAME"));
	            json.add(jo);
		}
		response.setContentType("text/html; charset=utf-8");
		response.getWriter().write("{"+"\""+"total"+"\""+":"+"\""+"5"+"\""+","+"\""+"root1"+"\""+":"+json.toString()+"}");				
		response.getWriter().flush();
	}
	/**
	 * 查找航线名称
	 * @throws Exception
	 */
	@RequestMapping(value="/findRoute")
	public void findRoute(HttpServletResponse response)throws Exception{
		List<PageData> entityMainTain = boatCompanyFixedSurchargeService.findRoute();
		JSONArray json = new JSONArray();
		for(PageData p : entityMainTain){
			 JSONObject jo = new JSONObject();
			 jo.put("ROUTE_NAME", p.get("ROUTE_NAME"));
	            json.add(jo);
		}
		response.setContentType("text/html; charset=utf-8");
		response.getWriter().write("{"+"\""+"total"+"\""+":"+"\""+"5"+"\""+","+"\""+"root2"+"\""+":"+json.toString()+"}");				
		response.getWriter().flush();
	}
	/**
	 * 查找费用名称
	 * @throws Exception
	 */
	@RequestMapping(value="/findFee")
	public void findFee(HttpServletResponse response)throws Exception{
		List<PageData> entityMainTain = boatCompanyFixedSurchargeService.findFee();
		JSONArray json = new JSONArray();
		for(PageData p : entityMainTain){
			 JSONObject jo = new JSONObject();
			 jo.put("FEE_NAME", p.get("FEE_NAME"));
	            json.add(jo);
		}
		response.setContentType("text/html; charset=utf-8");
		response.getWriter().write("{"+"\""+"total"+"\""+":"+"\""+"5"+"\""+","+"\""+"root3"+"\""+":"+json.toString()+"}");				
		response.getWriter().flush();
	}
	/**
	 * 查找船公司和结算单位 
	 * @throws Exception
	 */
	@RequestMapping(value="/findBoatCompanyAndUnit")
	public void findBoatCompanyAndUnit(HttpServletResponse response)throws Exception{
		List<PageData> entityMainTain = boatCompanyFixedSurchargeService.findBoatCompanyAndUnit();
		JSONArray json = new JSONArray();
		for(PageData p : entityMainTain){
			if(p!=null){
			 JSONObject jo = new JSONObject();
			 if(p.get("SETTLE_COMPANY")!=null && p.get("SETTLE_COMPANY")!=""){
				 jo.put("SETTLE_COMPANY", p.get("SETTLE_COMPANY")); 
			 }
			 json.add(jo);
			}
	            
		}
		response.setContentType("text/html; charset=utf-8");
		response.getWriter().write("{"+"\""+"total"+"\""+":"+"\""+"5"+"\""+","+"\""+"root4"+"\""+":"+json.toString()+"}");				
		response.getWriter().flush();
	}
	/**
	 * 保存信息
	 * @throws Exception
	 */
	@RequestMapping(value="/saveInfo")
	@ResponseBody
	public String saveInfo()throws Exception{
		PageData pd = new PageData();
		pd = this.getPageData();
		if(pd.getString("ID").isEmpty()){
			boatCompanyFixedSurchargeService.saveInfo(pd);
		}else{
			boatCompanyFixedSurchargeService.updateInfo(pd);
		}
		return null;
	}
	/**
	 * 删除信息
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/deleteInfo")
	@ResponseBody
	public String deleteInfo()throws Exception{
		PageData pd = new PageData();
		pd = this.getPageData();

		String data = pd.getString("array");

		JSONArray items = JSONArray.fromObject(data);
		for(int i=0,size=items.size();i<size;i++){
			JSONObject item = items.getJSONObject(i);
			String id = item.getString("id").trim();
			PageData pdq = new PageData();
			pdq.put("ID", id);
			boatCompanyFixedSurchargeService.deleteInfo(pdq);
		}
		return null;
		
	}
	
	/**
	 * 批量修改信息
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/updateInfo")
	@ResponseBody
	public String updateInfo()throws Exception{
		PageData pd = new PageData();
		pd = this.getPageData();

		String data = pd.getString("array");

		JSONArray items = JSONArray.fromObject(data);
		for(int i=0,size=items.size();i<size;i++){
			JSONObject item = items.getJSONObject(i);
			String id = item.getString("id").trim();
			PageData pdq = new PageData();
			pdq.put("ID", id);
			pdq.put("SETTLE_UNIT", item.getString("SETTLE_UNIT"));
			boatCompanyFixedSurchargeService.updataUnitInfo(pdq);
		}
		return null;
		
	}
	@Override
	public List getData(String type, Map param, Page page) {
		Map map = Maps.newHashMap();
		try {
			PageData pd = boatCompanyFixedSurchargeService.findInfoById(param.get("ID").toString());
			if(pd.getString("BOAT_COMPANY")!=null && pd.getString("BOAT_COMPANY")!=""){
				map.put("BOAT_COMPANY", pd.getString("BOAT_COMPANY"));
			}
			if(pd.getString("SETTLE_UNIT")!=null && pd.getString("SETTLE_UNIT")!=""){
				map.put("SETTLE_UNIT", pd.getString("SETTLE_UNIT"));
			}
			if(pd.getString("FEE_NAME")!=null && pd.getString("FEE_NAME")!=""){
				map.put("FEE_NAME", pd.getString("FEE_NAME"));
			}
			if(pd.get("FEE_PRICE")!=null && pd.get("FEE_PRICE")!=""){
				map.put("FEE_PRICE", pd.get("FEE_PRICE"));
			}
			
			if(pd.getString("CURRENCY_TYPE")!=null && pd.getString("CURRENCY_TYPE")!=""){
				map.put("CURRENCY_TYPE", pd.getString("CURRENCY_TYPE"));
			}
			if(pd.getString("CHARG_STANDARD")!=null && pd.getString("CHARG_STANDARD")!=""){
				map.put("CHARG_STANDARD", pd.getString("CHARG_STANDARD"));
			}
			if(pd.getString("BOX_TYPE")!=null && pd.getString("BOX_TYPE")!=""){
				map.put("BOX_TYPE", pd.getString("BOX_TYPE"));
			}
			if(pd.getString("REMARK")!=null && pd.getString("REMARK")!=""){
				map.put("REMARK", pd.getString("REMARK"));
			}

			if(pd.get("ENABLE")!=null && pd.get("ENABLE")!=""){
				pd.get("ENABLE").toString();

					map.put("ENABLE", pd.get("ENABLE"));

				
			}
			if(pd.getString("SEARCH_CODE")!=null && pd.getString("SEARCH_CODE")!=""){
				map.put("SEARCH_CODE", pd.getString("SEARCH_CODE"));
			}
			if(pd.getString("COMPANY_CODE")!=null && pd.getString("COMPANY_CODE")!=""){
				map.put("COMPANY_CODE", pd.getString("COMPANY_CODE"));
			}
			if(pd.getString("ROUTE_CODE")!=null && pd.getString("ROUTE_CODE")!=""){
				map.put("ROUTE_CODE", pd.getString("ROUTE_CODE"));
			}
			if(pd.getString("ROUTE_NAME")!=null && pd.getString("ROUTE_NAME")!=""){
				map.put("ROUTE_NAME", pd.getString("ROUTE_NAME"));
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return Lists.newArrayList(map);
	}
	@Override
	public String getService() {
		return  getClass().getSimpleName();
	}
	@Override
	public Map<String, String> setNames() {
		Map m = new HashMap(3);
		//海船公司固定附加费用
		m.put("boatCompanyFixed","boatCompanyFixed.xls");
		m.put("boatCompanyFixedName","海船公司固定附加费用("+ DateUtil.getSdfTimes()+").xls");
		m.put("boatCompanyFixedModule",getService());
		return m;
	}
	
	/**
	 * 
	 * @author zhouheyou
	 * 下载模版
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping(value="/downExcel")
	public void downExcel(HttpServletResponse response)throws Exception{
		FileDownload.fileDownload(response, PathUtil.getClasspath() + Const.FILEPATHFILE + "海船公司固定费用.xls", "海船公司固定费用.xls");
	}
	
	/**
	 * @author zhouheyou
	 * 导入
	 * @param file
	 * @return
	 * @throws Exception
	 */
		@RequestMapping(value="/boatCompanyFixesInfo")
		@ResponseBody
		public Object boatCompanyFixesInfo(@RequestParam("file") CommonsMultipartFile file) throws Exception {
			String filePath = PathUtil.getClasspath() + Const.FILEPATHFILE;
			String fileName =  FileUpload.fileUp(file, filePath, "test");	
			List<PageData> listPd = (List)ObjectExcelRead.readExcel(filePath, fileName, 1, 0, 0);
			PageData pdq = new PageData();
			for(PageData pd : listPd){
				if(pd.getString("var0")!=null && pd.getString("var0")!="" && pd.getString("var2")!=null && pd.getString("var2")!=""
						&& pd.getString("var3")!=null && pd.getString("var3")!="" && pd.getString("var7")!=null && pd.getString("var7")!=""){
					PageData pdq1 = new PageData();
					pdq1.put("BOAT_COMPANY", pd.getString("var0"));
					pdq1.put("SETTLE_UNIT", pd.getString("var2"));
					pdq1.put("FEE_NAME", pd.getString("var3"));
					pdq1.put("BOX_TYPE", pd.getString("var7"));
					if(pd.getString("var1")!=null && pd.getString("var1")!=""){
						pdq1.put("ROUTE_NAME", pd.getString("var1"));
					}
					PageData pdq2 = boatCompanyFixedSurchargeService.findInfoByInfo(pdq1);
					if(pdq2!=null){
						pdq.put("BOAT_COMPANY", pd.getString("var0"));
						pdq.put("ROUTE_NAME", pd.getString("var1"));
						pdq.put("SETTLE_UNIT", pd.getString("var2"));
						pdq.put("FEE_NAME", pd.getString("var3"));
						
						pdq.put("FEE_PRICE", pd.getString("var4"));
						pdq.put("CURRENCY_TYPE", pd.getString("var5"));
						pdq.put("CHARG_STANDARD", pd.getString("var6"));
						pdq.put("BOX_TYPE", pd.getString("var7"));
						
						pdq.put("REMARK", pd.getString("var8"));
						pdq.put("SEARCH_CODE", pd.getString("var9"));
						pdq.put("ENABLE", pd.getString("var10"));
						pdq.put("COMPANY_CODE", pd.getString("var11"));
						/**
						 * @author zhouheyou
						 */
						pdq.put("START_PRICE_TIME", pd.getString("var12"));//开始时间
						pdq.put("END_PRICE_TIME", pd.getString("var13"));//结束时间
						boatCompanyFixedSurchargeService.updateInfoByInfo(pdq);
					}else{
						pdq.put("BOAT_COMPANY", pd.getString("var0"));
						pdq.put("ROUTE_NAME", pd.getString("var1"));
						pdq.put("SETTLE_UNIT", pd.getString("var2"));
						pdq.put("FEE_NAME", pd.getString("var3"));
						
						pdq.put("FEE_PRICE", pd.getString("var4"));
						pdq.put("CURRENCY_TYPE", pd.getString("var5"));
						pdq.put("CHARG_STANDARD", pd.getString("var6"));
						pdq.put("BOX_TYPE", pd.getString("var7"));
						
						pdq.put("REMARK", pd.getString("var8"));
						pdq.put("SEARCH_CODE", pd.getString("var9"));
						pdq.put("ENABLE", pd.getString("var10"));
						pdq.put("COMPANY_CODE", pd.getString("var11"));
						/**
						 * @author zhouheyou
						 */
						pdq.put("START_PRICE_TIME", pd.getString("var12"));//开始时间
						pdq.put("END_PRICE_TIME", pd.getString("var13"));//结束时间
						boatCompanyFixedSurchargeService.saveInfo(pdq);
					}
				}
				
			}
			return ResponseResult.success(listPd);
			
		}
}
